<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅案JWT | MoneyLee’s Blog</title><meta name="author" content="李财"><meta name="copyright" content="李财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅JWT简介：讲解单机和分布式应⽤下登录校验，session共享，分布式缓存使⽤ 单机tomcat应⽤登录检验  sesssion保存在浏览器和应⽤服务器会话之间 ⽤户登录成功，服务端会保存⼀个session，当然客户端有⼀个sessionId 客户端会把sessionId保存在cookie中，每次请求都会携带这个sessionId   分布式应⽤中se">
<meta property="og:type" content="article">
<meta property="og:title" content="互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅案JWT">
<meta property="og:url" content="https://moneylee.gitee.io/blog/2020/02/26/%E4%BA%92%E8%81%94%E2%BD%B9%E9%A1%B9%E2%BD%AC%E4%B8%AD%20%E5%8D%95%E6%9C%BA%E5%92%8C%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E7%9A%84%E7%99%BB%E5%BD%95%E6%A0%A1%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85%E6%A1%88/index.html">
<meta property="og:site_name" content="MoneyLee’s Blog">
<meta property="og:description" content="互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅JWT简介：讲解单机和分布式应⽤下登录校验，session共享，分布式缓存使⽤ 单机tomcat应⽤登录检验  sesssion保存在浏览器和应⽤服务器会话之间 ⽤户登录成功，服务端会保存⼀个session，当然客户端有⼀个sessionId 客户端会把sessionId保存在cookie中，每次请求都会携带这个sessionId   分布式应⽤中se">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg">
<meta property="article:published_time" content="2020-02-26T11:12:02.000Z">
<meta property="article:modified_time" content="2020-12-18T00:43:01.000Z">
<meta property="article:author" content="李财">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://moneylee.gitee.io/blog/2020/02/26/%E4%BA%92%E8%81%94%E2%BD%B9%E9%A1%B9%E2%BD%AC%E4%B8%AD%20%E5%8D%95%E6%9C%BA%E5%92%8C%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E7%9A%84%E7%99%BB%E5%BD%95%E6%A0%A1%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85%E6%A1%88/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅案JWT',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-12-18 08:43:01'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="MoneyLee’s Blog" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2019/11/10/SYvXMgCFuTNOVeZ.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">47</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/bbs/"><i class="fa-fw fa fa-coffee"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 影音</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/blog/movies"><i class="fa-fw /movies/"></i><span> 1</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">MoneyLee’s Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/bbs/"><i class="fa-fw fa fa-coffee"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 影音</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music"><i class="fa-fw /music/"></i><span> 0</span></a></li><li><a class="site-page child" href="/blog/movies"><i class="fa-fw /movies/"></i><span> 1</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅案JWT</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-02-26T11:12:02.000Z" title="发表于 2020-02-26 19:12:02">2020-02-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-12-18T00:43:01.000Z" title="更新于 2020-12-18 08:43:01">2020-12-18</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅案JWT"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="互联⽹项⽬中-单机和分布式应⽤的登录校验解决⽅JWT"><a href="#互联⽹项⽬中-单机和分布式应⽤的登录校验解决⽅JWT" class="headerlink" title="互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅JWT"></a>互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅JWT</h2><h4 id="简介：讲解单机和分布式应⽤下登录校验，session共享，分布式缓存使⽤"><a href="#简介：讲解单机和分布式应⽤下登录校验，session共享，分布式缓存使⽤" class="headerlink" title="简介：讲解单机和分布式应⽤下登录校验，session共享，分布式缓存使⽤"></a>简介：讲解单机和分布式应⽤下登录校验，<strong>session</strong>共享，分布式缓存使⽤</h4><ul>
<li><p>单机tomcat应⽤登录检验</p>
<ul>
<li>sesssion保存在浏览器和应⽤服务器会话之间</li>
<li>⽤户登录成功，服务端会保存⼀个session，当然客户端有⼀个sessionId</li>
<li>客户端会把sessionId保存在cookie中，每次请求都会携带这个sessionId</li>
</ul>
</li>
<li><p>分布式应⽤中session共享</p>
<ul>
<li><p>真实的应⽤不可能单节点部署，所以就有个多节点登录session共享的问题需要解决</p>
</li>
<li><p>tomcat⽀持session共享，但是有⼴播⻛暴；⽤户量⼤的时候，占⽤资源就严重，不推荐</p>
</li>
<li><p>使⽤redis存储token:</p>
<ul>
<li><p>服务端使⽤UUID⽣成随机64位或者128位token，放⼊redis中，然后返回给客户端并存</p>
<p>储在cookie中</p>
</li>
<li><p>⽤户每次访问都携带此token，服务端去redis中校验是否有此⽤户即</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="分布式应⽤下登录检验解决⽅案-JWT讲解"><a href="#分布式应⽤下登录检验解决⽅案-JWT讲解" class="headerlink" title="分布式应⽤下登录检验解决⽅案 JWT讲解"></a>分布式应⽤下登录检验解决⽅案 <strong>JWT</strong>讲解</h3><h4 id="简介：分布式应⽤的登录检验解决⽅案-JWT讲解-json-wen-token"><a href="#简介：分布式应⽤的登录检验解决⽅案-JWT讲解-json-wen-token" class="headerlink" title="简介：分布式应⽤的登录检验解决⽅案 JWT讲解 json wen token"></a>简介：分布式应⽤的登录检验解决⽅案 <strong>JWT</strong>讲解 <strong>json wen token</strong></h4><ul>
<li><p>什么是JWT</p>
<ul>
<li><p>JWT 是⼀个开放标准，它定义了⼀种⽤于简洁，⾃包含的⽤于通信双⽅之间以 JSON 对象的</p>
<p>形式安全传递信息的⽅法。 可以使⽤ HMAC 算法或者是 RSA 的公钥密钥对进⾏签名</p>
</li>
<li><p>简单来说**:** 就是通过⼀定规范来⽣成<strong>token</strong>，然后可以通过解密算法逆向解密<strong>token</strong>，这样就</p>
<p>可以获取⽤户信息</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	id:<span class="number">888</span>,</span><br><span class="line">	name:<span class="string">&#x27;⼩D&#x27;</span>,</span><br><span class="line">	expire:<span class="number">10000</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">funtion 加密(object, appsecret)&#123;</span><br><span class="line"> <span class="function">xxxx</span></span><br><span class="line"><span class="function"> return <span class="title">base64</span><span class="params">( token)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function 解密(token ,appsecret)&#123;</span><br><span class="line"> xxxx</span><br><span class="line"> <span class="comment">//成功返回true,失败返回false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>优点</p>
<ul>
<li>⽣产的token可以包含基本信息，⽐如id、⽤户昵称、头像等信息，避免再次查库</li>
<li>存储在客户端，不占⽤服务端的内存资源</li>
</ul>
</li>
<li><p>缺点</p>
<ul>
<li><p>token是经过base64编码，所以可以解码，因此token加密前的对象不应该包含敏感信</p>
<p>息，如⽤户权限，密码等</p>
</li>
<li><p>如果没有服务端存储，则不能做登录失效处理，除⾮服务端改秘钥</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>JWT格式组成 头部、负载、签名</p>
<ul>
<li><p>header+payload+signature</p>
<ul>
<li><p>头部：主要是描述签名算法</p>
</li>
<li><p>负载：主要描述是加密对象的信息，如⽤户的id等，也可以加些规范⾥⾯的东⻄，如iss</p>
<p>签发者，exp 过期时间，sub ⾯向的⽤户</p>
</li>
<li><p>签名：主要是把前⾯两部分进⾏加密，防⽌别⼈拿到token进⾏base解密后篡改token</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>关于jwt客户端存储</p>
<ul>
<li>可以存储在cookie，localstorage和sessionStorage⾥⾯</li>
</ul>
</li>
</ul>
<h3 id="封装通用方法"><a href="#封装通用方法" class="headerlink" title="封装通用方法"></a>封装通用方法</h3><h4 id="引⼊相关依赖并开发JWT⼯具类-开发⽣产token和校验token的办法"><a href="#引⼊相关依赖并开发JWT⼯具类-开发⽣产token和校验token的办法" class="headerlink" title="引⼊相关依赖并开发JWT⼯具类**,** 开发⽣产token和校验token的办法"></a>引⼊相关依赖并开发<strong>JWT</strong>⼯具类**,** 开发⽣产<strong>token</strong>和校验<strong>token</strong>的办法</h4><ul>
<li>加⼊相关依赖</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- JWT相关 --&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.jsonwebtoken<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jjwt<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.7.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>封装⽣产token⽅法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据⽤户信息，⽣成令牌</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> user</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">geneJsonWebToken</span><span class="params">(User user)</span></span>&#123;</span><br><span class="line"> 	String token = Jwts.builder().setSubject(SUBJECT)</span><br><span class="line"> 	.claim(<span class="string">&quot;head_img&quot;</span>,user.getHeadImg())</span><br><span class="line"> 	.claim(<span class="string">&quot;id&quot;</span>,user.getId())</span><br><span class="line"> 	.claim(<span class="string">&quot;name&quot;</span>,user.getName())</span><br><span class="line"> 	.setIssuedAt(<span class="keyword">new</span> Date())</span><br><span class="line"> 	.setExpiration(<span class="keyword">new</span> Date(System.currentTimeMillis() + EXPIRE))</span><br><span class="line"> 	.signWith(SignatureAlgorithm.HS256,SECRET).compact();</span><br><span class="line"> 	token = TOKEN_PREFIX + token;</span><br><span class="line"> 	<span class="keyword">return</span> token;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>封装校验token⽅法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 校验token的⽅法</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> token</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Claims <span class="title">checkJWT</span><span class="params">(String token)</span></span>&#123;</span><br><span class="line"> 	<span class="keyword">try</span>&#123;</span><br><span class="line"> 	<span class="keyword">final</span> Claims claims = Jwts.parser().setSigningKey(SECRET)</span><br><span class="line"> 	.parseClaimsJws(token.replace(TOKEN_PREFIX,<span class="string">&quot;&quot;</span>)).getBody();</span><br><span class="line"> 	<span class="keyword">return</span> claims;</span><br><span class="line"> &#125;<span class="keyword">catch</span> (Exception e)&#123;</span><br><span class="line"> 	<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">李财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://moneylee.gitee.io/blog/2020/02/26/%E4%BA%92%E8%81%94%E2%BD%B9%E9%A1%B9%E2%BD%AC%E4%B8%AD%20%E5%8D%95%E6%9C%BA%E5%92%8C%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E7%9A%84%E7%99%BB%E5%BD%95%E6%A0%A1%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85%E6%A1%88/">https://moneylee.gitee.io/blog/2020/02/26/%E4%BA%92%E8%81%94%E2%BD%B9%E9%A1%B9%E2%BD%AC%E4%B8%AD%20%E5%8D%95%E6%9C%BA%E5%92%8C%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E7%9A%84%E7%99%BB%E5%BD%95%E6%A0%A1%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85%E6%A1%88/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://moneylee.gitee.io/blog" target="_blank">MoneyLee’s Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2020/05/07/SpringBoot%20%E8%B7%A8%E5%9F%9F%E9%85%8D%E7%BD%AE/"><img class="prev-cover" src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SpringBoot 跨域配置</div></div></a></div><div class="next-post pull-right"><a href="/blog/2020/02/03/iOS13%E9%80%82%E9%85%8D%E6%B7%B1%E8%89%B2%E6%A8%A1%E5%BC%8F/"><img class="next-cover" src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">iOS13适配深色模式</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2019/11/10/SYvXMgCFuTNOVeZ.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">李财</div><div class="author-info__description">嗨，我是李财 (MoneyLee)， 一名来自中国的 iOS开发者。</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">47</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/licai28" target="_blank" title=""><i class="fa fa-github"></i></a><a class="social-icon" href="mailto:licai_1024@hotmail.com" target="_blank" title=""><i class="fa fa-envelope"></i></a><a class="social-icon" href="/blog/img/mywechat.jpg" target="_blank" title=""><i class="fa fa-weixin"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%92%E8%81%94%E2%BD%B9%E9%A1%B9%E2%BD%AC%E4%B8%AD-%E5%8D%95%E6%9C%BA%E5%92%8C%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E7%9A%84%E7%99%BB%E5%BD%95%E6%A0%A1%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85JWT"><span class="toc-number">1.</span> <span class="toc-text">互联⽹项⽬中 单机和分布式应⽤的登录校验解决⽅JWT</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B%EF%BC%9A%E8%AE%B2%E8%A7%A3%E5%8D%95%E6%9C%BA%E5%92%8C%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E4%B8%8B%E7%99%BB%E5%BD%95%E6%A0%A1%E9%AA%8C%EF%BC%8Csession%E5%85%B1%E4%BA%AB%EF%BC%8C%E5%88%86%E5%B8%83%E5%BC%8F%E7%BC%93%E5%AD%98%E4%BD%BF%E2%BD%A4"><span class="toc-number">1.0.1.</span> <span class="toc-text">简介：讲解单机和分布式应⽤下登录校验，session共享，分布式缓存使⽤</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E4%B8%8B%E7%99%BB%E5%BD%95%E6%A3%80%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85%E6%A1%88-JWT%E8%AE%B2%E8%A7%A3"><span class="toc-number">1.1.</span> <span class="toc-text">分布式应⽤下登录检验解决⽅案 JWT讲解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%80%E4%BB%8B%EF%BC%9A%E5%88%86%E5%B8%83%E5%BC%8F%E5%BA%94%E2%BD%A4%E7%9A%84%E7%99%BB%E5%BD%95%E6%A3%80%E9%AA%8C%E8%A7%A3%E5%86%B3%E2%BD%85%E6%A1%88-JWT%E8%AE%B2%E8%A7%A3-json-wen-token"><span class="toc-number">1.1.1.</span> <span class="toc-text">简介：分布式应⽤的登录检验解决⽅案 JWT讲解 json wen token</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%85%E9%80%9A%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">封装通用方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%95%E2%BC%8A%E7%9B%B8%E5%85%B3%E4%BE%9D%E8%B5%96%E5%B9%B6%E5%BC%80%E5%8F%91JWT%E2%BC%AF%E5%85%B7%E7%B1%BB-%E5%BC%80%E5%8F%91%E2%BD%A3%E4%BA%A7token%E5%92%8C%E6%A0%A1%E9%AA%8Ctoken%E7%9A%84%E5%8A%9E%E6%B3%95"><span class="toc-number">1.2.1.</span> <span class="toc-text">引⼊相关依赖并开发JWT⼯具类**,** 开发⽣产token和校验token的办法</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/" title="无题"><img src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/img/default.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/blog/2021/09/14/Flutter%20%E5%90%AF%E5%8A%A8%E9%A1%B5%E9%85%8D%E7%BD%AE/" title="无题">无题</a><time datetime="2021-09-14T03:05:25.000Z" title="发表于 2021-09-14 11:05:25">2021-09-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="iOS机器学习"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="iOS机器学习"/></a><div class="content"><a class="title" href="/blog/2020/08/21/iOS%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="iOS机器学习">iOS机器学习</a><time datetime="2020-08-21T02:04:10.000Z" title="发表于 2020-08-21 10:04:10">2020-08-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" title="Docker 基础知识整理"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl4zhjzuj30qy0e0gyz.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Docker 基础知识整理"/></a><div class="content"><a class="title" href="/blog/2020/08/20/Docker%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" title="Docker 基础知识整理">Docker 基础知识整理</a><time datetime="2020-08-20T12:12:02.000Z" title="发表于 2020-08-20 20:12:02">2020-08-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/19/Flutter/" title="Flutter"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Flutter"/></a><div class="content"><a class="title" href="/blog/2020/08/19/Flutter/" title="Flutter">Flutter</a><time datetime="2020-08-19T02:04:03.000Z" title="发表于 2020-08-19 10:04:03">2020-08-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/08/19/iOS%20AR%E5%92%8CARKit4/" title="iOS AR和ARKit4"><img src="https://tva1.sinaimg.cn/large/006y8mN6ly1g8vl0019wlj31ej0u0x6q.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="iOS AR和ARKit4"/></a><div class="content"><a class="title" href="/blog/2020/08/19/iOS%20AR%E5%92%8CARKit4/" title="iOS AR和ARKit4">iOS AR和ARKit4</a><time datetime="2020-08-19T02:04:03.000Z" title="发表于 2020-08-19 10:04:03">2020-08-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 李财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>